class Solution1 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) {
            return null;
        }
        if(p==root||q==root) {
            return root;
        }
        TreeNode leftRoot=lowestCommonAncestor(root.left,p,q);
        TreeNode rightRoot=lowestCommonAncestor(root.right,p,q);
        if(leftRoot != null && rightRoot != null) {
            return root;
        } else if(leftRoot != null) {
            return leftRoot;
        } else if(rightRoot != null) {
            return rightRoot;
        }
        return null;
    }
}
public class test {
}
